package org.jradweb.tools.generator.maven;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.hibernate.HibernateException;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.jradweb.tools.generator.hibernate.Hbm2DDL;
import org.jradweb.tools.generator.utils.PropertiesLoader;

/**
 * Goal generator ddl from hibernate
 * 
 * @goal hbm2ddl
 * 
 */
public class Hbm2DDLMojo extends BaseMojo {

	public static final String DATABASE_DRIVER_CLASS_NAME = "database.driverClassName";

	public static final String DATABASE_URL = "database.url";

	public static final String DATABASE_USER_NAME = "database.userName";

	public static final String DATABASE_PASSWORD = "database.password";

	public static final String DATABASE_SCHEMA_NAME = "database.schemaNames";

	public static final String DATABASE_DIALECT = "database.dialect";

	@Override
	public void doExecute() throws MojoExecutionException, MojoFailureException {

		Hbm2DDL h2d = new Hbm2DDL(this.getBasedir(), this.getLog());

		h2d.toDDL();
	}

	public static void main(String[] args) {
		try {
			Hbm2DDLMojo mojo = new Hbm2DDLMojo();
			mojo.execute();
		} catch (MojoExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MojoFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/*
	 * load properties from ${artifactId}.properties
	 */
	private Properties loadProperties() throws FileNotFoundException,
			IOException {
		String configHome = System.getProperty("WEB_CONFIG_HOME");
		String propFile = configHome + "/test-"
				+ this.getProject().getArtifactId() + ".properties";
		this.getLog().info("load properties from " + propFile);
		java.util.Properties props = new Properties();
		props.load(new FileInputStream(new File(propFile)));

		this.getLog().info(
				"database.driverClassName="
						+ props.getProperty("database.driverClassName"));
		this.getLog().info("database.url=" + props.getProperty("database.url"));
		this.getLog().info(
				"database.userName=" + props.getProperty("database.userName"));
		this.getLog().info(
				"database.password=" + props.getProperty("database.password"));
		this.getLog().info(
				"database.schemaNames="
						+ props.getProperty("database.schemaNames"));
		this.getLog().info(
				"database.dialect=" + props.getProperty("database.dialect"));
		return props;

	}

}
